<?php
$customerServices = new CustomerServices();	
$bankAccountService = new BankAccountServices();
switch ($action) {
	case "add":
		if($permit == true)
		{
			echo "<pre>";
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			var_dump($data);
			$data['comm'] = Tool::convertMoneyToNumber($data['comm']);
			$customerDomain = new CustomerDomain($data);

			$bankAccountDomain1 = new BankaccountDomain(null);
			$bankAccountDomain1->setAccountNumber($data['accountNumber1']);
			$bankAccountDomain1->setBankID($data['customerBank1']);
			$bankAccountID1 = $bankAccountService->create($context, $result, $bankAccountDomain1);
			$customerDomain->setBankAccountID1($bankAccountID1);

			$bankAccountDomain2 = new BankaccountDomain(null);
			$bankAccountDomain2->setAccountNumber($data['accountNumber2']);
			$bankAccountDomain2->setBankID($data['customerBank2']);
			$bankAccountID2 = $bankAccountService->create($context, $result, $bankAccountDomain2);
			$customerDomain->setBankAccountID2($bankAccountID2);
			
			
			$userDomain = new UserDomain($data);
			$customer = new Customer($customerDomain);
			$customer->setUserDomain($userDomain);
			$customerServices->createNewCustomer($context, $result, $customer);		
			if($result->isFailed())
				echo "<script>alert('".$result->getMessage()."');window.location='index.php?func=cus&opt=list';</script>";			
			header("Location:index.php?func=cus&opt=list");
		}
		else
			header("Location:index.php?func=dash");
	break;
	
	case "delete":
		if($permit == true)
		{
			if(isset($_GET['id']))
			{				
				$customer = $customerServices->getCustomerByOid($context, $result, $_GET['id']);
				$customerServices->deleteCustomer($context, $result, $customer);
				header("Location:index.php?func=cus&opt=list");
			}								
		}else
			header("Location:index.php?func=dash");
	break;
	
	case "edit":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			$data['comm'] = Tool::convertMoneyToNumber($data['comm']);
			echo "<pre>";
			$customerDomain	= new CustomerDomain($data);
			$customer = $customerServices->getCustomerByOid($context, $result, $data['id']);
			
			$bankAccountDomain1 = new BankaccountDomain(null);
			$bankAccountDomain1->setAccountNumber($data['accountNumber1']);
			$bankAccountDomain1->setBankID($data['customerBank1']);
			
			if($customer->getCustomerDomain()->getBankAccountID1() != "null")
			{
				$bankAccountDomain1->setBankAccountID($customer->getCustomerDomain()->getBankAccountID1());				
				$bankAccountService->update($context, $result, $bankAccountDomain1);
				$customerDomain->setBankAccountID1($customer->getCustomerDomain()->getBankAccountID1());	
			}
			else 
			{
				$bankAccountID1 = $bankAccountService->create($context, $result, $bankAccountDomain1);
				$customerDomain->setBankAccountID1($bankAccountID1);				
			}
			
			$bankAccountDomain2 = new BankaccountDomain(null);
			$bankAccountDomain2->setAccountNumber($data['accountNumber2']);
			$bankAccountDomain2->setBankID($data['customerBank2']);
			if($customer->getCustomerDomain()->getBankAccountID2() != "null")
			{
				$bankAccountDomain2->setBankAccountID($customer->getCustomerDomain()->getBankAccountID2());				
				$bankAccountService->update($context, $result, $bankAccountDomain2);
				$customerDomain->setBankAccountID2($customer->getCustomerDomain()->getBankAccountID2());	
			}
			else 
			{
				$bankAccountID2 = $bankAccountService->create($context, $result, $bankAccountDomain2);
				$customerDomain->setBankAccountID2($bankAccountID2);				
			}
			
			$customer->setCustomerDomain($customerDomain);
			$customerServices->updateCustomer($context, $result, $customer);
			header("Location:index.php?func=cus&opt=list");
		}
		else
			header("Location:index.php?func=dash");
	break;
	
	default:
		echo "khong co tinh nang nay";
	break;
}
?>					